package com.zsrd.lessons.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsrd.lessons.domain.TsLessonsHistory;
import com.zsrd.lessons.domain.dto.TsLessonsHistoryDto;
import com.zsrd.lessons.mapper.TsLessonsHistoryMapper;
import com.zsrd.lessons.service.TsLessonsHistoryService;
import com.zsrd.lessons.service.TsLessonsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 课程访问历史表 服务实现类
 * </p>
 *
 * @author Chen Wei
 * @since 2022-07-11
 */
@Service
public class TsLessonsHistoryServiceImpl extends ServiceImpl<TsLessonsHistoryMapper, TsLessonsHistory> implements TsLessonsHistoryService {
    @Autowired
    private TsLessonsService tsLessonsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void readLessons(String loginId, Long id) {
        TsLessonsHistory tsLessonsHistory = new TsLessonsHistory();
        tsLessonsHistory.setCtime(LocalDateTime.now());
        tsLessonsHistory.setLid(id);
        tsLessonsHistory.setUid(loginId);
        save(tsLessonsHistory);
        tsLessonsService.updateViews(id);
    }

    @Override
    public IPage<TsLessonsHistoryDto> getReadPeopleList(Long id, Long current, Long size) {
        Page<TsLessonsHistoryDto> page = new Page<>(current, size);
        IPage<TsLessonsHistoryDto> p = this.baseMapper.getReadPeopleList(page, id);
        return p;
    }
}
